package cetcbigdata.da.service.beijing.facilitation;

import cetcbigdata.da.base.Base;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.nodes.Element;

import java.util.ArrayList;
import java.util.List;

/**
 * @author YuanGongPing
 * @version 0.1
 * @description Spider
 * @since 2022/9/16 14:33
 */


public class SpiderBeiJing extends Base {
    private static String region = "北京市";

    private static String module = "服务事项-便利化清单";

    @Override
    public JSONObject getDataDemo() {
        try {
            return processResult("xxx", "xxx", "xxx", "xxx", new JSONObject(), "xxx", "xxx");
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {
        List<JSONObject> startUrlList = startUrls();
        for(JSONObject urlItem : startUrlList){
            String name = urlItem.getString("name");
            String listUrl = urlItem.getString("url");
            if(name.equals("证照分离")){
                String listResponse = getReq(listUrl, getHeaders(), getParams());
                JXDocument jxDocument = new JXDocument(listResponse);
                List<Object> rs = jxDocument.sel("//div[@class='wai_box']//a");
                for (Object o : rs){
                    JSONObject outItem = new JSONObject();
                    String fileName  = ((Element) o).text();
                    String href = ((Element) o).attr("href");
                    String fileUrl  = "https://banshi.beijing.gov.cn" + href;
                    outItem.put("文件名", fileName);
                    outItem.put("文件地址", fileUrl );
                    outItem.put("minioId", UtilBeiJing.fileDownloadAndUploadMinioGetImage(
                            fileUrl, href, getHeaders(), getParams()
                    ) );
                    processItem(
                            processResult(
                                    listUrl, listResponse, "", "",
                                    outItem, region, module + "-" + name
                            ),
                            recordSender, fieldsList
                    );
                }
            }else {
                String listResponse = getReq(listUrl, getHeaders(), getParams());
                JSONArray listPageData = JSONObject.parseArray(listResponse);
                for(Object o : listPageData){
                    JSONObject dataItem = (JSONObject) o;
                    JSONObject outItem = new JSONObject();
                    outItem.put("办理部门", dataItem.getString("exam_department"));
                    outItem.put("事项名称", dataItem.getString("matter_name"));
                    if(dataItem.containsKey("window")){
                        outItem.put("窗口", dataItem.getString("window"));
                    }
                    if(dataItem.containsKey("url")){
                        outItem.put("办理网址", dataItem.getString("url"));
                    }
                    processItem(
                            processResult(
                                    listUrl, listResponse, "", "",
                                    outItem, region, module + "-" + name
                            ),
                            recordSender, fieldsList
                    );
                }
            }
        }
    }

    private static List<JSONObject> startUrls(){
        List<JSONObject> out = new ArrayList<>();
        // 首先获取采集
        String indexUrl = "https://banshi.beijing.gov.cn/pubtask/facilitation.html?locationCode=110000000000";
        String response = getReq(indexUrl, getHeaders(), getParams());

        List<JSONObject> startUrlList = UtilBeiJing.getUrls(response);
        for(JSONObject urlItem : startUrlList){
            String name = urlItem.getString("name");
            String starturl = urlItem.getString("url");
            starturl = starturl.replace("/pubtask/", "").replace(".html", "");
            if(name.equals("证照分离")){
                String jsUrl = "https://banshi.beijing.gov.cn/pubtask/zzfl.html?locationCode=110000000000";
                JSONObject outItem = new JSONObject();
                outItem.put("name", name);
                outItem.put("url", jsUrl);
                out.add(outItem);
            }else {
                String jsUrl = "https://banshi.beijing.gov.cn/pubtask/js/" + starturl + ".js";
                response = getReq(jsUrl, getHeaders(), getParams());
                String jsonAddr = UtilBeiJing.getJsonAddr(response);
                JSONObject outItem = new JSONObject();
                outItem.put("name", name);
                outItem.put("url", "https://banshi.beijing.gov.cn" + jsonAddr);
                out.add(outItem);
            }


        }
        return out;
    }

    private static JSONObject getHeaders(){
        JSONObject headers = new JSONObject();
        headers.put("Referer", "https://banshi.beijing.gov.cn/pubtask/ygzwjbml.html?locationCode=110000000000");
        headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36");
        return headers;
    }

    private static JSONObject getParams(){
        return new JSONObject();
    }

    public static void main(String[] args) {
        SpiderBeiJing spider = new SpiderBeiJing();
        spider.getData(null, new ArrayList<>());
    }
}
